import 'package:flutter/material.dart';
import 'package:better_player_plus/better_player_plus.dart';

///视频播发器
class BetterPlayerPlusPage extends StatefulWidget {
  const BetterPlayerPlusPage({super.key});

  @override
  State<BetterPlayerPlusPage> createState() => _BetterPlayerPlusPageState();
}

class _BetterPlayerPlusPageState extends State<BetterPlayerPlusPage> {
  late BetterPlayerController _betterPlayerController;

  @override
  void initState() {
    super.initState();

    BetterPlayerDataSource bpds = BetterPlayerDataSource(
      BetterPlayerDataSourceType.network,
      "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
      subtitles: BetterPlayerSubtitlesSource.single(
          type: BetterPlayerSubtitlesSourceType.network,
          selectedByDefault: true,
          url:
              "https://dl.dropboxusercontent.com/s/71nzjo2ux3evxqk/example_subtitles.srt"),
      // subtitles: [
      //   BetterPlayerSubtitlesSource(
      //     type: BetterPlayerSubtitlesSourceType.network,
      //     name: "EN",
      //     urls: [
      //       "https://dl.dropboxusercontent.com/s/71nzjo2ux3evxqk/example_subtitles.srt"
      //     ],
      //   ),
      //   BetterPlayerSubtitlesSource(
      //     type: BetterPlayerSubtitlesSourceType.network,
      //     name: "DE",
      //     urls: [
      //       "https://dl.dropboxusercontent.com/s/71nzjo2ux3evxqk/example_subtitles.srt"
      //     ],
      //   ),
      // ],
    );

    _betterPlayerController = BetterPlayerController(
        BetterPlayerConfiguration(
          autoPlay: true,
          placeholder: Text("ni baba"),
        ),
        betterPlayerDataSource: bpds);

    // _betterPlayerController.setupDataSource(betterPlayerDataSource);

    _betterPlayerController.addEventsListener((event) {
      print("Better player event: ${event.betterPlayerEventType}");

      if (event.betterPlayerEventType == BetterPlayerEventType.progress) {
        final position = event.parameters?["progress"] as Duration?;
        final duration = event.parameters?["duration"] as Duration?;

        print("当前播放进度: $position / 总时长: $duration");
      }

      if (event.betterPlayerEventType == BetterPlayerEventType.exception) {
        final error = event.parameters?["exception"] ?? "Unknown error";
        print("❌ 播放异常: $error");
        // 你也可以在这里执行错误上报、弹出提示、重新播放等逻辑
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text("播放出错：$error")),
        );
      }
    });

    //_betterPlayerController.getAspectRatio();
  }

  @override
  void dispose() {
    _betterPlayerController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _buildBody,
    );
  }

  get _buildBody => Stack(
        children: [
          Center(
            child: AspectRatio(
              aspectRatio: 16 / 9,
              child: BetterPlayer(
                controller: _betterPlayerController,
              ),
            ),
          ),
        ],
      );
}
